package com.example.codereader;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class CodeCanvas extends View {

    private static final String TAG = "CodeCanvas";

    //  单位宽度
    public int width = 8;
    public int height = 400;
    public int height_short = 320;

    private Paint paint;

    private int origin_x = 350, origin_y = 100;
    private int pos_x, pos_y;

    private char[] chs = {};
    private Rect[] rects = new Rect[54];
    private int nRect = 0;

    public void setUnitWidth(int width) {
        this.width = width;
    }

    public CodeCanvas(Context context) {
        super(context);


        pos_x = origin_x;
        pos_y = origin_y;
        paint = new Paint();
        for (int i = 0; i < rects.length; i++) {
            rects[i] = new Rect();
        }
    }

    public CodeCanvas(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        pos_x = origin_x;
        pos_y = origin_y;
        paint = new Paint();
        for (int i = 0; i < rects.length; i++) {
            rects[i] = new Rect();
        }
    }

    public CodeCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        pos_x = origin_x;
        pos_y = origin_y;
        paint = new Paint();
        for (int i = 0; i < rects.length; i++) {
            rects[i] = new Rect();
        }
    }

    public CodeCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        pos_x = origin_x;
        pos_y = origin_y;
        paint = new Paint();
        for (int i = 0; i < rects.length; i++) {
            rects[i] = new Rect();
        }
    }

    public void SetBarCode(String code) {
        chs = code.toCharArray();
        super.invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        nRect = 0;
        pos_x = origin_x;
        for (char ch : chs) {
            if (ch == '0') // void
                flush(canvas, false, false);
            else if (ch == '1') // fill
                flush(canvas, true, false);
            else if (ch == 'v') // void+len
                flush(canvas, false, true);
            else if (ch == 'f') // fill+len
                flush(canvas, true, true);
        }
    }

    public void flush(Canvas canvas, boolean _fill, boolean _long) {
        int realH = _long ? this.height : this.height_short;
        if (_fill && nRect < rects.length) {
            rects[nRect].set(this.pos_x, this.pos_y,
                    this.pos_x + this.width, this.pos_y + realH);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(rects[nRect], paint);
            nRect++;
        }
        this.pos_x += this.width;
    }

}
